import { ref } from 'vue'
import { ElMessage } from 'element-plus'
export const callApi = async (apiFn, scene, type = 'text', errorText = '接口调用失败，请重试', successText = '') => {
  const loading = ref(true)
  const result = { content: '', imageList: [], error: '' }
  try {
    const res = await apiFn({ scene })
    if (type === 'text' ||  type === 'table') {
      result.content = res.data.result || res.data.content || '无数据'
      result.imageList = []
    } else if (type === 'image') {
      result.imageList = res.data.images || []
      result.content = ''
    } else {
      console.log()
    }
    if (successText) 
      ElMessage.success(successText)
  } catch (err) {
    result.error = errorText
  } finally {
    loading.value = false
  }
  return { ...result, loading: loading.value }
}